<?php

defined('IN_IA') or exit('Access Denied');


require 'model/phpQuery.php';
require 'model/QueryList.php';

include "lib/Wxadoc.php";
include "lib/function.php";
include "model/manger.mod.php";
include "model/rising.mod.php";
include "lib/WxpayAPI_php_v3/WxpayAPI_php_v3.php";

class bh_risingModuleSite extends WeModuleSite
{

    public $manger= '';
    public $rising = '';

    public $siteRoot = '';

    public $w = array();
    public $gpc = array();

    protected  $filter = array(
        'ph0001.hezyq.com',
    );
    /**
     * bh_voteModuleSite constructor.
     */
    public function __construct()
    {
        global $_GPC, $_W;
        $this->gpc = $_GPC;
        $this->w = $_W;
        define('UNIACID', $this->w['uniacid']);
        $this->manger = new manger();
        $this->rising = new rising();
        $this->siteRoot = $this->w['siteroot'];

    }

    public function settingTemplate($template)
    {
        $this->__define = IA_ROOT . "/addons/bh_manger/site.php";
        $siteRoot = $this->siteRoot;
        $modular_name = $this->gpc['m'];
        include $this->template($template);

    }

    /**
     *  基本配置
     */
    public function doWebConfig()
    {
        $siteRoot = $this->siteRoot;
        $modular_name = $this->gpc['m'];
        $crontab = $this->w['siteroot'] . "app/index.php?i={$this->w['uniacid']}&from=wxapp&c=entry&a=wxapp&do=crontab&m=bh_rising";
        $qqCrontab = $this->w['siteroot'] . "app/index.php?i={$this->w['uniacid']}&from=wxapp&c=entry&a=wxapp&do=qqCrontab&m=bh_rising";
        $batch = $this->w['siteroot'] . "app/index.php?i={$this->w['uniacid']}&from=wxapp&c=entry&a=wxapp&do=batch&m=bh_rising";
        $keys = array(
            'ordinary' => array(
                array('key' => 'xcx_title'),
                array('key' => 'audit_model', 'default' => 1),
                array('key' => 'currency_name'),                                    
                array('key' => 'own_currency'),
                array('key' => 'one_currency'),
                array('key' => 'two_currency'),
                array('key' => 'two_currency'),
                array('key' => 'currency_money','default' => 0.01),
                array('key' => 'clock_num'),
                array('key' => 'clock_interval'),
                array('key' => 'block_msg', 'default' => "您的账号存在异常,请联系客服"),
                array('key' => 'clock_text', 'default' => "点击打卡"),
                array('key' => 'reward_type', 'default' => 1),
                array('key' => 'is_bonus_deduction', 'default' => 1),
                array('key' => 'batch_start_currency', 'default' => 1),
                array('key' => 'batch_end_currency', 'default' => 100),
                array('key' => 'show_friends_num', 'default' => 499),
                array('key' => 'pool', 'default' => 100),
                array('key' => 'ad_type', 'default' => 1),
                array('key' => 'pattern', 'default' => 1),
                array('key' => 'task_bonus', 'default' => 1),
                array('key' => 'exchange_type', 'default' => 1),
                array('key' => 'xcx_display_type', 'default' => 1),
                array('key' => 'is_bonus', 'default' => 1),
                array('key' => 'abnormal_abnormal', 'default' => 5),
                array('key' => 'click_num_bonus', 'default' => 1),
                array('key' => 'is_captcha', 'default' => 1),
                array('key' => 'rank_number', 'default' => 100),
                array('key' => 'rank_money', 'default' => 0.03),

            ),
            'share' =>array(
                array('key' => 'share_text'),
                array('key' => 'share_image'),
            ),
            'redPackets' => array(
                array('key' => 'red_video_open'),
                array('key' => 'red_number'),
            ),
            'pdd' => array(
                array('key' => 'popular_recommend'),
                array('key' => 'pdd_switch'),
                array('key' => 'pdd_navigation_name'),
                array('key' => 'pdd_client_id'),
                array('key' => 'pdd_client_secret'),
                array('key' => 'pdd_pid', 'default'=> '8311880_50547417'),
                array('key' => 'ppd_commission', 'default'=> 10),
            ),
            'withdrawals' => array(
                array('key' => 'withdrawal_state'),
                array('key' => 'first_presentation'),
                array('key' => 'cash_reviewed'),
                array('key' => 'follow_up_presentation'),
                array('key' => 'bag_daily_upper'),
                array('key' => 'withdrawal_day_frequency'),
                array('key' => 'sign_days'),
                array('key' => 'cash_type'),
                array('key' => 'cash_bg'),
                array('key' => 'cash_explain'),
                array('key' => 'apiclient_cert'),
                array('key' => 'apiclient_key'),
                array('key' => 'my_withdrawal_txt', 'default' => '现金奖励'),
                array('key' => 'withdrawal_button_txt', 'default' => '提现'),

            ),
            'answer' => array(
                array('key' => 'answer_reward', 'default' => 3),
                array('key' => 'answer_status', 'default' => 2),
                array('key' => 'answer_status', 'default' => 2),
                array('key' => 'answer_double', 'default' => 2),
            ),
            'diyUi' => array(
                array('key' => 'home_bg_image'),
                array('key' => 'baduanjin_images'),
                array('key' => 'login_image'),
                array('key' => 'my_bg'),
                array('key' => 'poster_bg'),
                array('key' => 'pop_gold'),
                array('key' => 'official_account_img'),
                array('key' => 'nav_bg_color'),
                array('key' => 'roll_content_bg'),
                array('key' => 'click_resize'),
                array('key' => 'click_bg_color'),
                array('key' => 'new_home_bg_image'),
                array('key' => 'new_home_image'),
                array('key' => 'currency_image'),
                array('key' => 'new_home_txt'),
                array('key' => 'new_home_txt_color'),
            ),
            'advertising' => array(
                array('key' => 'screen_ad'),
                array('key' => 'grid_ad'),
                array('key' => 'video_ad'),
                array('key' => 'banner_id'),
                array('key' => 'click_ad_double',  'default' => 1),
                array('key' => 'full_screen_ad'),
                array('key' => 'single_ad'),
                array('key' => 'ad_time', 'default' => 5),
            ),
            'qq' => array(
                array('key' => 'contact_type','default' => 1),
                array('key' => 'home_group_id'),
                array('key' => 'qq_mch_id'),
                array('key' => 'qq_mch_key'),
                array('key' => 'op_user_id'),
                array('key' => 'op_user_passwd'),
                array('key' => 'qq_group_id'),
                array('key' => 'qq_apiclient_cert'),
                array('key' => 'qq_apiclient_key'),
                array('key' => 'qq_profit_subscribe_id'),
                array('key' => 'customer_img'),
                array('key' => 'qq_earnings_send_hour'),
                array('key' => 'qq_earnings_send_minute'),
                array('key' => 'guard_popularity', 'default' => 10),
                array('key' => 'popularity_share_text'),
                array('key' => 'popularity_share_images'),
                array('key' => 'first_show_img'),

            ),
            'message' => array(
                array('key' => 'profit_subscribe_id'), // 收益到账
                array('key' => 'earnings_send_hour'),
                array('key' => 'earnings_send_minute'),
            ),
            'jd' => array(
                array('jd_apikey'),
            )
        );

        $op = $this->get('op', 'ordinary');
        if (!isset($keys[$op])) {
            message('参数错误', $this->createWebUrl('config'));
        }

        if ($this->w['ispost']) {

            foreach ($keys[$op] as $value) {
                $names = array('share_image');
                if (!in_array($value['key'], array('apiclient_cert', 'apiclient_key', 'qq_apiclient_cert', 'qq_apiclient_key'))) {
                    $val = in_array($value['key'], $names) ? $this->get($value['key']) : $this->get($value['key']);
                    p_update('config', array('value' => $val), array('key' => $value['key']));
                }

                if ($value['key'] == 'exchange_note') {
                    $exchange_note = $this->get('exchange_note');
                    p_update('config', array('value' => json_encode($exchange_note)), array('key' => $value['key']));
                    continue;
                }

                if ($value['key'] == 'baduanjin_images') {
                    $baduanjin_images = $this->get('baduanjin_images');
                    p_update('config', array('value' => json_encode($baduanjin_images)), array('key' => $value['key']));
                    continue;
                }

                if ($value['key'] == 'cash_money') {
                    $cash_money = $this->get('cash_money');
                    p_update('config', array('value' => json_encode($cash_money)), array('key' => $value['key']));
                    continue;
                }


                if ($value['key'] == 'cash_explain') {
                    $cash_explain = $this->get('cash_explain');
                    p_update('config', array('value' => json_encode($cash_explain)), array('key' => $value['key']));
                    continue;
                }

                if ($value['key'] == 'pdd_pid') {
                    //$this->pddMandate($this->get('pdd_pid'));
                }
            }

            $path1 = ATTACHMENT_ROOT . $this->w['uniacid'] . 'cert/';
            if (!file_exists($path1)) {
                @mkdir($path1,0777,true);
            }


            if (isset($_FILES['_apiclient_cert']['tmp_name']) && file_exists($_FILES['_apiclient_cert']['tmp_name'])) {
                $path = $path1 . 'apiclient_cert.pem';

                if (move_uploaded_file($_FILES['_apiclient_cert']['tmp_name'], $path)) {
                    p_update('config', array('value' => $path), array('key' => 'apiclient_cert'));
                }
            }
            if (isset($_FILES['_apiclient_key']['tmp_name']) && file_exists($_FILES['_apiclient_key']['tmp_name'])) {
                $path = $path1 . 'apiclient_key.pem';
                if (move_uploaded_file($_FILES['_apiclient_key']['tmp_name'], $path)) {
                    p_update('config', array('value' => $path), array('key' => 'apiclient_key'));
                }
            }


            if (isset($_FILES['_qq_apiclient_cert']['tmp_name']) && file_exists($_FILES['_qq_apiclient_cert']['tmp_name'])) {
                $path = $path1 . 'apiclient_cert.pem';
                if (move_uploaded_file($_FILES['_qq_apiclient_cert']['tmp_name'], $path)) {
                    p_update('config', array('value' => $path), array('key' => 'qq_apiclient_cert'));
                }
            }
            if (isset($_FILES['_qq_apiclient_key']['tmp_name']) && file_exists($_FILES['_qq_apiclient_key']['tmp_name'])) {
                $path = $path1 . 'apiclient_key.pem';
                if (move_uploaded_file($_FILES['_qq_apiclient_key']['tmp_name'], $path)) {
                    p_update('config', array('value' => $path), array('key' => 'qq_apiclient_key'));
                }
            }


        }
        $initial = function ($key, $default = 0){
            $config = p_get('config', array('key' => $key));
            if (empty($config)) {
                $config = array(
                    'key' => $key,
                    'value' => $default
                );
                p_insert('config', $config);
            }
            return $config;
        };
        foreach ($keys[$op] as $value) {
            $k = $value['key'];
            $$k = $initial($k, $value['default']);
        }

        if ($op == 'diyUi') {
            $baduanjin_images['value'] = !empty($baduanjin_images['value']) ? json_decode($baduanjin_images['value'], true) : array(0 => '');
        }
        if ($op == 'ordinary') {
            $exchange_note['value'] = !empty($exchange_note['value']) ? json_decode($exchange_note['value'], true) : array(0 => '');
            $exchange_amount['value'] = !empty($exchange_amount['value']) ? json_decode($exchange_amount['value'], true) : array(0 => '');
        }

        if ($op == 'withdrawals') {
            $cash_explain['value'] = !empty($cash_explain['value']) ? json_decode($cash_explain['value'], true) : array(0 => '');
            $cash_money['value'] = !empty($cash_money['value']) ? json_decode($cash_money['value'], true) : array(0 => '');
        }

        $isSupportQQ = isSupportQQ() ? 1 : 0;
        $url = pathinfo($this->siteRoot);
        $filterUrl = false;
        if (in_array($url['basename'], $this->filter)) {
            $filterUrl =  true;
        }

        $basename = pathinfo($this->w["siteroot"]);
        $basename = $basename["basename"];
        $w7_order_id = p_get('config', array('key' => "w7_order_id"));
        $activation_code = p_get('config', array('key' => "activation_code"));
        $w7_order_ids = '123456';
        if(empty($w7_order_id)){
              p_insert('config', array("key" => "w7_order_id", "value" => md5($basename . "1fdfd4c01357071e9fa6328a9d330c" . $w7_order_ids)));
        }else{
              p_update('config', array('value' => md5($basename . "1fdfd4c01357071e9fa6328a9d330c" . $w7_order_ids)), array('key' => 'w7_order_id'));
        }
        $w7_order_id = p_get('config', array('key' => "w7_order_id"));
        if(empty($activation_code)){
              p_insert('config', array("key" => "activation_code", "value" => md5($basename . "1fdfd4c01357071e9fa6328a9d330c" . $w7_order_id)));
        }else{
              p_update('config', array('value' => md5($basename . "1fdfd4c01357071e9fa6328a9d330c" . $w7_order_id)), array('key' => 'activation_code'));
        }

        $send = $this->rising->isSend() ? 1 : 0;

        $isDouble = $this->rising->isDouble() ? 1 : 0;

        $isPopularitye = $this->rising->isPopularitye();

        include $this->template('config');
    }



    /**
     * 用户管理
     */
    public function doWebMember()
    {
        $page   = $this->get('page');
        $where  = "head != '' ".get_uniacid();
        $id = $this->get('id');
        $member_id = $this->get('member_id');
        $op = $this->get('op');
        if ($op == 'delete') {
            $bool = p_delete('member', array('id' => $id));
            p_delete('day', array('member_id' => $id));
            json('', $bool ? 1 : 0);
        } elseif ($op == 'updatePhone') {
            $phone = $this->get('phone');
            if(preg_match("/^1[34578]{1}\d{9}$/",$phone)){
                $bool = p_update('member', array('phone' => $phone), array('id' => $id));
                json('', $bool ? 1 : 0);
            } else {
               json('', 0);
            }
        } elseif($op == 'block'){
            $bool = p_update('member',array('is_block' => 2), array('id' => $id));
            json('', $bool ? 1 : 0);
        } elseif($op == 'cancelBlock'){
            $bool = p_update('member',array('is_block' => 1), array('id' => $id));
            json('', $bool ? 1 : 0);
        } else {
            $nickname = $this->get('nickname');
            if ($nickname) {
                $where .= " AND nickname LIKE '%{$nickname}%'";
            }
            if ($member_id) {
                $where .= " AND  id ={$member_id}";
            }
            if ($id) {
                $where .= " AND parent_id={$id}";
            }
            $time = $this->get('time');
            if (!$time) {
                $time = array('start' => '2019-08-01','end' => date('Y-m-d', time())) ;
            }
            $is_fictitious = $this->get('is_fictitious', 0);
            if ($is_fictitious !=0) {
                $where .= " AND is_fictitious = {$is_fictitious}";
            }
            $start = strtotime($time['start']);
            $end = strtotime($time['end']) + 86400;
            $where .= " AND created > {$start} AND created <= {$end}";
            $filed = "id";
            if ($op == 'money_sort' || $op == 'money_r_sort') {
                $filed = 'money';
            }
            if ($op == 'remind_sort' || $op == 'remind_r_sort') {
                $filed = 'remind';
            }
            if ($op == 'currency_sort' || $op == 'currency_r_sort') {
                $filed = 'currency';
            }
            if ($op == 'abnormal_sort' || $op == 'abnormal_r_sort') {
                $filed = 'abnormal';
            }
            $orderBy = "ASC";
            if ($op == 'money_r_sort' ) {
                $orderBy = "DESC";
            }
            if ($op == 'remind_r_sort') {
                $orderBy = "DESC";
            }
            if ($op == 'currency_r_sort') {
                $orderBy = "DESC";
            }
            if ($op == 'abnormal_r_sort') {
                $orderBy = "DESC";
            }
            $member = $this->manger->getTableList('member',$page, $where, $filed, $orderBy);
            if ($member['list']) {
                foreach ($member['list'] as & $value) {
                    $value['inviter']    = p_getcolumn('member', array('id' => $value['parent_id']), 'nickname');
                    $value['groupCount'] = p_getcolumn('group', array('member_id' => $value['id']), 'COUNT(*)') ? : 0;
                    $value['taskCount']  = p_getcolumn('task', array('member_id' => $value['id']), 'COUNT(*)') ? : 0;
                    $value['dayCount']   = p_getcolumn('day', array('member_id' => $value['id']), 'SUM(clock)') ? : 0;
                    $value['dayNumber']   = p_getcolumn('day', array('member_id' => $value['id']), 'COUNT(*)') ? : 0;
                    $value['inviteNumber'] = p_getcolumn('member', array('parent_id' => $value['id']), 'COUNT(*)') ? : 0;
                }
            }
            $date = date('Y-m-d', time());
            $todayWhere = $this->manger->getTime($date, $date, 'created');
            $todayWhere .= " AND is_fictitious =1 AND uniacid=". UNIACID;
            $total       = p_getcolumn('member', array('is_fictitious' => 1), 'COUNT(*)');
            $todayMember = p_fetchcolumn("SELECT COUNT(*) FROM ".prefix('member')." WHERE {$todayWhere}");
            $isSupportQQ = isSupportQQ() ? 1 : 0;
            $isPopularitye = $this->rising->isPopularitye();
            include $this->template('member');
        }

    }

    public function doWebChangePayCode()
    {
        $op = $this->get('op');

        $id  = $this->get('id');
        if ($op == 'edit') {

            $bool = p_update('member', array('payment_code' => $this->get('payment_code')), array('id' => $id));
            message($bool ? '操作成功': '操作失败', $this->createWebUrl('member'), $bool ? 'success': 'error');
        }  else {
            $member = p_get('member', array('id' => $id));
            include $this->template('change_pay_code');
        }
    }
    public function doWebFictitious()
    {
        $table = prefix('member', true);
        $where = "uniacid={$this->w['uniacid']} AND is_fictitious = 2";
        $pindex = max(1, intval($this->get('page')));
        $psize = 20;
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == "addshow") {
            include $this->template('fictitious_add');
        } elseif ($op == 'add' || $op == 'edit') {
            $data = array(
                'nickname'      => $this->get('nickname'),
                'head'          => tomedia($this->get('head')),
                'currency'      => $this->get('currency'),
                'created'       => time(),
                'openid'        => $this->getIssueNumber(12) . $this->w['uniacid'],
                'is_fictitious' => 2
            );
            $bool = !empty($id) ? p_update('member', $data , array('id' => $id)) : p_insert('member', $data);
            message($bool ? '操作成功': '操作失败', $this->createWebUrl('fictitious'), $bool ? 'success': 'error');
        } elseif ($op == 'edit_show'){
            $id = $this->get('id');
            $memberData = p_get('member', array('id' => $id));
            include $this->template('fictitious_add');
        }  elseif ($op == 'delete') {
            $id = $this->get('id');
            p_delete('member', array('id' => $id));
            json('1');
        }else {
            $list = pdo_fetchall('SELECT * FROM '.$table." WHERE {$where} ORDER BY id DESC LIMIT ".($pindex - 1) * $psize.','.$psize);
            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM {$table} WHERE {$where}");
            $pager = pagination($total, $pindex, $psize);
            include $this->template('fictitious');

        }

    }




    public function doWebRoll()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == "addShow" || $op == 'editShow') {
            $roll = p_get('roll', array('id' => $id));
            include $this->template('roll_add');
        } elseif ($op == 'add' || $op == 'edit') {
            $data = array(
                'image'      => $this->get('image'),
                'content'    => $this->get('content'),

            );
            $bool = !empty($id) ? p_update('roll', $data , array('id' => $id)) : p_insert('roll', $data);
            message($bool ? '操作成功': '操作失败', $this->createWebUrl('roll'), $bool ? 'success': 'error');
        }  elseif ($op == 'delete') {
            $id = $this->get('id');
            p_delete('roll', array('id' => $id));
            json('1');
        }else {
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            $roll = $this->manger->getTableList('roll',$page, $where);
            include $this->template('roll');
        }

    }



    public function doWebCondition()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == "addShow" || $op == 'editShow') {
            $condition = p_get('withdrawals_condition', array('id' => $id));
            include $this->template('condition_add');
        } elseif ($op == 'add' || $op == 'edit') {
            $data = array(
                'number'      => $this->get('number'),
                'click_day'   => $this->get('click_day'),
                'share_num'   => $this->get('share_num'),
                'money'       => $this->get('money'),
            );
            if (!$id) {
                $data['created'] = time();
            }
            $bool = !empty($id) ? p_update('withdrawals_condition', $data , array('id' => $id)) : p_insert('withdrawals_condition', $data);
            message($bool ? '操作成功': '操作失败', $this->createWebUrl('condition'), $bool ? 'success': 'error');
        }  elseif ($op == 'delete') {
            $id = $this->get('id');
            p_delete('withdrawals_condition', array('id' => $id));
            json('1');
        }else {
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            $condition = $this->manger->getTableList('withdrawals_condition',$page, $where);
            include $this->template('condition');
        }

    }



    public function doWebBatch()
    {
        $member  = p_fetchall('SELECT * FROM '.prefix('member')." WHERE is_fictitious = 1 ORDER BY id DESC LIMIT 0, 100");
        if ($member) {
            foreach ($member as $value) {
                $data = array(
                    'nickname' => '达人'.mt_rand(1, 9999),
                    'head'     => $value['head'],
                    'currency' => mt_rand(10, 900),
                    'created'  => time(),
                    'openid'   => mt_rand(1, 3679) . $this->getIssueNumber(8) . $this->w['uniacid'],
                    'is_fictitious' => 2
                );

                p_insert('member', $data);
            }
        }
        message('操作成功', $this->createWebUrl('fictitious'), 'success');
    }
    public function doWebMoneyLog()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == 'delete') {
            $bool = p_delete('money_log', array('id' => $id));
            json('', $bool ? 1 : 0);
        }  else {
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            $nickname = $this->get('nickname');
            $member_id = $this->get('member_id');
            if ($nickname) {
                $where .= " AND member_id in ({$this->manger->getMemberId($nickname)})";
            }
            if ($member_id) {
                $where .= " AND member_id ={$member_id}";
            }
            $moneyLog = $this->manger->getTableList('money_log',$page, $where);
            if ($moneyLog['list']) {
                $type = array(
                    1 => '分红',
                    2 => '提现',
                    3 => '首次打卡赠送',
                    4 => '兑换红包商品',
                    5 => '兑换商品'
                );
                foreach ($moneyLog['list'] as & $value) {
                    $value['nickname'] = p_getcolumn('member', array('id' => $value['member_id']), 'nickname');
                    $value['type']     = $type[$value['type']];
                }
            }
            include $this->template('money_log');
        }

    }


    public function doWebGraphic()
    {
        $siteRoot = $this->siteRoot;
        $modular_name = $this->gpc['m'];
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == 'delete') {
            $bool = p_delete('new', array('id' => $id));
            json('', $bool ? 1 : 0);
        } elseif($op == 'addShow' || $op == 'editShow'){
            $graphic = p_get('new', array('id' => $id));
            include $this->template('graphic_add');
        }  elseif($op == 'add' || $op == 'edit') {

            $data = array(
                'title' => $this->get('title'),
                'subtitle' => $this->get('subtitle'),
                'cover_image' => $this->get('cover_image'),
                'author' => $this->get('author'),
                'author_image' => $this->get('author_image'),
                'browse' => $this->get('browse'),
                'content' => $this->get('content'),
                'state' => $_POST['state'],
                'sort' => $this->get('sort'),
                'created' => strtotime($this->get('created')),
            );

            $bool = $id ? p_update('new', $data, array('id' => $id)) : p_insert('new', $data);
            message($bool ? '操作成功': '操作失败', $this->createWebUrl('graphic'), $bool ? 'success': 'error');
        } else{
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            $nickname = $this->get('nickname');
            if ($nickname) {
                $where .= " AND member_id in ({$this->manger->getMemberId($nickname)})";
            }
            $graphic = $this->manger->getTableList('new',$page, $where);

            include $this->template('graphic');
        }

    }

    public function doWebSystemDay()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == 'delete') {
            $bool = p_delete('system_day', array('id' => $id));
            json('', $bool ? 1 : 0);
        } else {
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            $nickname = $this->get('nickname');
            if ($nickname) {
                $where .= " AND member_id in ({$this->manger->getMemberId($nickname)})";
            }
            $system_day = $this->manger->getTableList('system_day',$page, $where);
            if ($system_day['list']) {
                $type = array(
                    1 => '分红',
                    2 => '提现',
                );
                foreach ($system_day['list'] as & $value) {
                    $value['type']     = $type[$value['type']];
                }
            }
            $graphic = $this->rising->isGraphic() ? 1 : 0;
            include $this->template('system_day');
        }

    }


    public function doWebDay()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == 'delete') {
            $bool = p_delete('day', array('id' => $id));
            json('', $bool ? 1 : 0);
        } else {
            $page   = $this->get('page');
            $where  = "member_id = {$id} ".get_uniacid();

            $day = $this->manger->getTableList('day',$page, $where);
            if ($day['list']) {
                $status = array(
                    1 => '未结算',
                    2 => '已结算',
                );
                foreach ($day['list'] as & $value) {
                    $value['nickname'] = p_getcolumn('member', array('id' => $value['member_id']), 'nickname');
                    $value['type']     = $status[$value['status']];
                }
            }
            include $this->template('day');
        }

    }

    public function doWebCurrencyLog()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == 'delete') {
            $bool = p_delete('currency_log', array('id' => $id));
            json('', $bool ? 1 : 0);
        } else {
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            $nickname = $this->get('nickname');
            $member_id = $this->get('member_id');
            if ($nickname) {
                $where .= " AND member_id in ({$this->manger->getMemberId($nickname)})";
            }

            if ($member_id) {
                $where .= " AND member_id = {$member_id}";
            }
            $currencyLog = $this->manger->getTableList('currency_log',$page, $where);
            if ($currencyLog['list']) {
                $type = array(
                    1 => '自己打卡',
                    2 => '一级好友',
                    3 => '二级好友打卡',
                    4 => '虚拟用户打卡',
                    5 => '兑换商品',
                    6 => '分红扣除',
                    7 => '完成任务'
                );
                foreach ($currencyLog['list'] as & $value) {
                    $value['nickname'] = p_getcolumn('member', array('id' => $value['member_id']), 'nickname');
                    $value['type']     =$type[$value['type']];
                }
            }
            $date = date('Y-m-d', time());
            $todayWhere = $this->manger->getTime($date, $date, 'created');
            $todayWhere .= "  AND type in (1,2,3,4,5) AND uniacid=". UNIACID;
            $total       = p_getcolumn('currency_log', array(), 'SUM(currency)');
            $todayMember = p_fetchcolumn("SELECT SUM(currency) FROM ".prefix('currency_log')." WHERE {$todayWhere}");


            $yesterday_start = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')-1,date('Y')));
            $yesterday_end = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d'),date('Y'))-1);

            $yesterdayWhere = $this->manger->getTime($yesterday_start, $yesterday_end, 'created');
            $yesterdayWhere .= " AND type in (1,2,3,4,5) AND  uniacid=". UNIACID;
            $yesterday = p_fetchcolumn("SELECT SUM(currency) FROM ".prefix('currency_log')." WHERE {$yesterdayWhere}");
            include $this->template('currency_log');
        }

    }

    public function doWebPhoneBill()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == 'delete') {
            $bool = p_delete('phone_bill', array('id' => $id));
            json('', $bool ? 1 : 0);
        } elseif($op == 'success') {
            $bool = p_update('phone_bill', array('status' => 2),array('id' => $id));
            json('', $bool ? 1 : 0);
        } elseif($op == 'waring'){
            $bool = p_update('phone_bill', array('status' => 3),array('id' => $id));
            json('', $bool ? 1 : 0);
        } elseif($op == 'successPay') {
            $member = p_get('phone_bill', array('id' => $id));
            $returnData = cost($member['phone'], $member['money']);
            if ($returnData['error_code'] == -1000) {
                json($returnData['reason'],  0);
            }
            p_update('phone_bill', array('status' => 2),array('id' => $id));
            json('', 1);
        } else {
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            $nickname = $this->get('nickname');
            if ($nickname) {
                $where .= " AND member_id in ({$this->manger->getMemberId($nickname)})";
            }
            if ($status = $this->get('status')){
                $where .= " AND status ={$status}";
            }
            $phoneBill = $this->manger->getTableList('phone_bill',$page, $where);
            if ($phoneBill['list']) {
                $status = array(
                    1 => '未充值',
                    2 => '已经充值',
                    3 => '拒绝',
                );
                foreach ($phoneBill['list'] as & $value) {
                    $value['nickname'] = p_getcolumn('member', array('id' => $value['member_id']), 'nickname');
                    $value['statusStr']  = $status[$value['status']];
                }
            }
            include $this->template('phone_bill');
        }

    }

    public function doWebGroup()
    {
        $id = $this->get('id');
        $op = $this->get('op');
        if ($op == 'delete') {
            $bool = p_delete('group', array('id' => $id));
            json('', $bool ? 1 : 0);
        } else {
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            if ($id) {
                $where .= " AND member_id = {$id} ";
            }
            $group = $this->manger->getTableList('group',$page, $where);
            if ($group['list']) {
                foreach ($group['list'] as & $value) {
                    $value['nickname'] = p_getcolumn('member', array('id' => $value['member_id']), 'nickname');
                    $value['inviter'] = p_getcolumn('member', array('id' => $value['downline_id']), 'nickname');
                }
            }
            include $this->template('group');
        }

    }
    public function doWebSignIn()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == 'delete') {
            $bool = p_delete('signin', array('id' => $id));
            json('', $bool ? 1 : 0);
        } else {
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            $nickname = $this->get('nickname');
            if ($nickname) {
                $where .= " AND member_id in ({$this->manger->getMemberId($nickname)})";
            }
            $signIn = $this->manger->getTableList('signin',$page, $where);
            if ($signIn['list']) {
                foreach ($signIn['list'] as & $value) {
                    $value['nickname'] = p_getcolumn('member', array('id' => $value['member_id']),'nickname');
                }
            }
            include $this->template('signIn');
        }

    }
    private function setPayment()
    {
        WxPayConfig::$APPID = $this->w['uniaccount']['key'];
        WxPayConfig::$MCHID = $this->w['account']['setting']['payment']['wechat']['mchid'];
        WxPayConfig::$KEY = $this->w['account']['setting']['payment']['wechat']['signkey'];
        WxPayConfig::$APPSECRET = $this->w['uniaccount']['secret'];

        $apiclient_cert = getConfig('apiclient_cert');
        if (!empty($apiclient_cert)) {
            WxPayConfig::$SSLCERT_PATH = $apiclient_cert;
        }
        $apiclient_key = getConfig('apiclient_key');
        if (!empty($apiclient_key)) {
            WxPayConfig::$SSLKEY_PATH = $apiclient_key;
        }
    }




    /**
     *  提现管理
     */
    public function doWebWithdrawals()
    {
        $siteRoot   = $this->siteRoot;
        $modular_name = $this->gpc['m'];
        $op = $this->get('op');
        if ($op == 'sgree') {
            $id = $this->get('id');
            $cash = p_get('withdrawals', array('id' => $id));
            if ($cash['status'] > 1) {
                json(1);
            }
            $cash_type = getConfig('cash_type', 1);
            if ($cash_type == 2 ) {
                p_update('withdrawals', array('status' => 2), array('id' => $id));
                json(1);
            } else {
                pdo_begin();
                try{
                    $this->setPayment();
                    if (!p_update('withdrawals' , array('status' => 2, 'success_time' => time()), array('id' => $id, 'status' => 1))) {
                        throw new Exception('提现失败');
                    }
                    if (getConfig('qq_mch_id')) {
                        if (!$this->rising->qqCash($cash['member_id'], $cash['money'],'', $cash['id'])) {
                            throw new Exception($this->rising->error);
                        }
                    } else {
                        if (!$this->rising->cash($cash['money'], $cash['member_id'])) {
                            throw new Exception($this->rising->error);
                        }
                    }

                    pdo_commit();
                    json(1);
                } catch (Exception $e) {
                    pdo_rollback();
                    json($e->getMessage(), 0);
                }
            }

        } elseif ($op == 'refuse') {
            $id = $this->get('id');
            $cash = p_get('withdrawals', array('id' => $id));
            if ($cash['status'] > 1) {
                json(1);
            }
            p_update('withdrawals', array('status' => 3), array('id' => $id));
            json(1);
        } else {
            $table = prefix('withdrawals');
            $where = "uniacid=" . UNIACID;
            $status = $this->get('status', 0);
            if ($status > 0) {
                $where .= " AND status={$status}";
            }
            $nickname = $this->get('nickname');
            if ( $nickname ) {
                $where .=  " AND member_id in ({$this->manger->getMemberId($nickname)}) ";
            }
            $pindex = max(1, intval($this->get('page')));
            $psize = 20;
            $list = p_fetchall("SELECT * FROM {$table} WHERE {$where}  ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize.','.$psize);
            $statusStr = array(
                1 => '申请成功',
                2 => '提现成功',
                3 => '提现失败'
            );
            if ($list) {
                foreach ($list as & $value) {
                    $value['nickname'] = p_getcolumn('member', array('id' => $value['member_id']), 'nickname');
                    $value['type'] = $value['type'] == 1 ? '企业零钱': '收款码';
                    $value['statusStr'] = $statusStr[$value['status']];
                }
            }
            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM {$table} WHERE {$where}");
            $pager = pagination($total, $pindex, $psize);

            $totalMoney       = p_getcolumn('withdrawals', array('status' => 2), 'SUM(money)');
            $date = date('Y-m-d', time());

            $todayWhere = $this->manger->getTime($date, $date, 'created');
            $todayWhere .= " AND status =2 AND uniacid =". UNIACID;
            $todayMoney = p_fetchcolumn("SELECT COUNT(*) FROM ".prefix('withdrawals')." WHERE {$todayWhere}");

            $todayWhere1 = $this->manger->getTime($date, $date, 'created');
            $todayWhere1 .= " AND status =1 AND uniacid =". UNIACID;
            $todayMoney1 = p_fetchcolumn("SELECT COUNT(*) FROM ".prefix('withdrawals')." WHERE {$todayWhere1}");

            $todayWhere3 = $this->manger->getTime($date, $date, 'created');
            $todayWhere3 .= " AND status = 3 AND uniacid =". UNIACID;
            $todayMoney3 = p_fetchcolumn("SELECT COUNT(*) FROM ".prefix('withdrawals')." WHERE {$todayWhere3}");

            $balance   = p_getcolumn('withdrawals', array('status' => 1), 'SUM(money)');
            include $this->template('withdrawals');
        }
    }

    public function doWebTreasure()
    {
        $op = $this->get('op');
        if ($op == 'addshow') {
            include $this->template('treasure_add');
        } elseif($op == 'add' || $op == 'edit') {
            $id = $this->get('id');
            $data = array(
                'name' => $this->get('name'),
                'cover_image' => $this->get('cover_image'),
                'issue_number' => $this->getIssueNumber(4),
                'number_copies' => $this->get('number_copies'),
                'image' => json_encode($this->get('image')),
                'price' => $this->get('price'),
                'max_number' => $this->get('max_number'),
                'is_down' => $this->get('is_down', 1),
                'type' => $this->get('type', 1),
                'created' => time(),
            );
            if ($data['type'] == 2) {
                $data['pwd'] = $this->get('pwd');
            }
            if ($id) {
                message(p_update('treasure', $data ,array('id' => $id)) ? "修改成功": "修改失败", $this->createWebUrl('treasure'));
            } else {
                $data['issue_number'] = $this->checkCode($data['issue_number']);
                message(p_insert('treasure', $data) ? "添加成功": "添加失败", $this->createWebUrl('treasure'));
            }
        } elseif($op == "sort") {
            $id = $this->get('id');
            $sort = $this->get('sort');
            $bool =p_update('treasure', array('sort' => $sort), array('id' => $id));
            json('', $bool ? 1 : 0);
        } elseif ($op == "delete"){
            json('', p_update('treasure', array('is_delete' => 2), array('id' => $this->get('id'))) ? 1 : 0);
        } elseif ($op == 'editshow') {
            $treasure = p_get('treasure', array('id' => $this->get('id')));
            $treasure['image_data'] = $treasure ? json_decode($treasure['image']) : array();

            include $this->template('treasure_edit');
        } elseif ($op == 'treasure_member') {
            $pindex = max(1, intval($this->get('page')));
            $psize = 20;
            $treasure_id = $this->get('id');
            $table = prefix('treasure_code');
            $where = "uniacid={$this->w['uniacid']}";
            if ($treasure_id) {
                $where .= " AND treasure_id ={$treasure_id} ";
            }
            $name = $this->get('name');
            $nickname = $this->get('nickname');
            $member_id = $this->get('member_id');
            if ($member_id) {
                $where .= " AND member_id ={$member_id} ";
            }
            if ($nickname) {
                $member_ids = $this->manger->getMemberId($nickname);
                if ($member_ids) {
                    $where .= " AND member_id in ({$member_ids})";
                }
            }
            if ($name)  {
                $treasure_ids = $this->getTreasureId($name);
                if ($treasure_ids) {
                    $where .= " AND treasure_id in ({$treasure_ids})";
                }
            }
            $list = p_fetchall("SELECT * FROM {$table} WHERE {$where} ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize.','.$psize);
            if ($list) {
                foreach ($list as & $value) {
                    $value['member'] = p_get('member', array('id' => $value['member_id']), array('nickname', 'id'));
                    $value['treasure'] = p_get('treasure', array('id' => $value['treasure_id']), array('name'));
                    $value['treasure_member'] = p_get('treasure_member', array('id' => $value['treasure_member_id']), array('number_copies'));

                }
            }
            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM {$table} WHERE {$where}");
            $pager = pagination($total, $pindex, $psize);
            include $this->template('treasure_member');
        } elseif($op == 'default'){
            $id= $this->get('id');

            $treasure = p_get('treasure_code', array('id' => $id));
            p_update('treasure_code', array('is_appoint' => 1), array('treasure_id' => $treasure['treasure_id'], 'is_appoint' => 2));

            $bool = p_update('treasure_code', array('is_appoint' => 2), array('id' => $id));
            message($bool? '指定成功':'指定失败',$this->createWebUrl('treasure'));
        } else {
            $pindex = max(1, intval($this->get('page')));
            $psize = 20;
            $table = prefix('treasure');
            $where = "uniacid={$this->w['uniacid']} AND is_delete =1";
            $list = p_fetchall("SELECT * FROM {$table} WHERE {$where} ORDER BY sort DESC LIMIT " . ($pindex - 1) * $psize.','.$psize);
            if ($list) {
                $treasure_code = prefix('treasure_code');
                foreach ($list as & $value) {
                    $value['treasure_member'] = pdo_fetchcolumn("SELECT COUNT(*) FROM {$treasure_code} WHERE  uniacid={$this->w['uniacid']} AND treasure_id = {$value['id']}");
                    $value['shop_name'] = p_getcolumn('merchant', array('id' => $value['shop_id']), 'shop_name');
                }
            }
            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM {$table} WHERE {$where}");
            $pager = pagination($total, $pindex, $psize);
            include $this->template('treasure');
        }

    }

    public function doWebCategory()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == 'addshow' || $op == 'editshow') {
            $category = p_get('category', array('id' => $id));
            include $this->template('category_add');
        } elseif ($op == 'add' || $op == 'edit') {
            $data = array(
                'category_name' => $this->get('category_name'),
                'category_describe' => $this->get('category_describe'),
                'states' => $this->get('states'),
                'sort' => $this->get('sort'),
            );
            $bool = $id ? p_update('category', $data, array('id' => $id)) : p_insert('category', $data);
            message($bool ? "操作成功": "操作失败", $this->createWebUrl('category'));
        } elseif($op == 'sort') {
            $sort = (int)$this->get('sort');
            $bool = p_update('category', array('sort' =>$sort ), array('id'=> $id));
            json('', $bool ? 1 : 0);
        } elseif ($op == 'delete'){
            $bool = p_update('category', array('is_delete' => 2), array('id'=> $id));
            json('', $bool ? 1 : 0);
        } else {
            $page   = $this->get('page');
            $where  = "is_delete = 1 ".get_uniacid();
            $category = $this->manger->getTableList('category',$page, $where);
            include $this->template('category');
        }

    }




    public function doWebTask()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == 'addshow' || $op == 'editshow') {
            $task = p_get('task', array('id' => $id));
            include $this->template('task_add');
        } elseif ($op == 'add' || $op == 'edit') {
            $data = array(
                'type'    => $this->get('type'),
                'number'  => $this->get('number'),
                'title'   => $this->get('title'),
                'currency'=> $this->get('currency'),
                'appid'   => $this->get('appid'),
                'path'    => $this->get('path'),
                'sort'    => $this->get('sort'),
            );

            if (!$id) {
                $data['created'] = time();
            }
            $bool = $id ? p_update('task', $data, array('id' => $id)) : p_insert('task', $data);
            message($bool ? "操作成功": "操作失败", $this->createWebUrl('task'));
        } elseif($op == 'sort') {
            $sort = (int)$this->get('sort');
            $bool = p_update('task', array('sort' =>$sort ), array('id'=> $id));
            json('', $bool ? 1 : 0);
        } elseif ($op == 'delete'){
            $bool = p_delete('task', array('id' => $id));
            json('', $bool ? 1 : 0);
        } else {
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            $task = $this->manger->getTableList('task',$page, $where);
            if ($task['list']) {
                foreach ($task['list'] as & $value) {
                    $value['clickNumber'] = p_getcolumn('task_member', array('task_id' => $value['id']), 'COUNT(*)');
                }
            }
            include $this->template('task');
        }

    }


    public function doWebTaskClick()
    {
        $page   = $this->get('page');
        $id   = $this->get('id');
        $where  = "id = {$id} ".get_uniacid();
        $task = $this->manger->getTableList('task_member',$page, $where);
        if ($task['list']) {
            foreach ($task['list'] as & $value) {
                $value['nickname'] = p_getcolumn('member', array('id' => $value['member_id']), 'nickname');
                $value['taskTitle'] = p_getcolumn('task', array('id' => $value['task_id']), 'title');
            }
        }
        include $this->template('task_member');
    }
    public function doWebGoods()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        $siteRoot = $this->siteRoot;
        $modular_name = $this->gpc['m'];
        $isSupportQQ = isSupportQQ() ? 1 : 0;

        $isPopularitye = $this->rising->isPopularitye();
        $isQqhome = $this->rising->isQqhome();
        if ($op == 'addshow' || $op == 'editshow') {
            $category = p_getall('category', array('is_delete' => 1));
            if (empty($category)) {
                message("请选添加商品分类", $this->createWebUrl('category', array('op' => 'addshow')), 'warning');
            }
            $goods = p_get('goods', array('id' => $id));
            $goods['image'] = json_decode($goods['image'], true);
            include $this->template('goods_add');

        } elseif ($op == 'add' || $op == 'edit') {
            $data = array(
                'category_id'   => $this->get('category_id'),
                'type'          => $this->get('type'),
                'original_price'=> $this->get('original_price'),
                'goods_name'    => $this->get('goods_name'),
                'allow_type'   => $this->get('allow_type'),
                'exchange_type' => $this->get('exchange_type'),
                'money'         => $this->get('money'),
                'exchange_number' => $this->get('exchange_number'),
                'cover_image' => $this->get('cover_image'),
                'image'     => json_encode($this->get('image')),
                'is_free'   => $this->get('is_free'),
                'free'      => $this->get('free'),
                'inventory_type' => $this->get('inventory_type'),
                'inventory' => $this->get('inventory'),
                'introduce' => $this->get('introduce'),
                'allow_number' => $this->get('allow_number'),
                'status'    => $this->get('status'),
                'bag_money' => $this->get('bag_money'),
                'sort'      => $this->get('sort'),
            );
            if (!$id){
                $data['created']= time();
            }
            if ($data['type'] ==3) {
                $data['fabulous'] = $this->get('fabulous');
            }
            if ($isQqhome) {
                $data['is_home'] = $this->get('is_home');
            }
            $bool = $id ? p_update('goods', $data, array('id' => $id)) : p_insert('goods', $data);
            message($bool ? "操作成功": "操作失败", $this->createWebUrl('goods'));
        } elseif($op == 'sort') {
            $sort = (int)$this->get('sort');
            $bool = p_update('goods', array('sort' =>$sort ), array('id'=> $id));
            json('', $bool ? 1 : 0);
        } elseif($op == 'delete'){
            $bool = p_update('goods', array('is_delete' => 2), array('id'=> $id));
            json('', $bool ? 1 : 0);
        } else {
            $page   = $this->get('page');
            $where  = "is_delete = 1 ".get_uniacid();
            $goods = $this->manger->getTableList('goods',$page, $where);
            if ($goods['list']) {
                $type = array(
                    1 => '实物',
                    2 => '红包',
                    3 => '点赞'
                );
                foreach ($goods['list'] as & $value) {
                    $value['category'] = p_getcolumn('category', array('id' => $value['category_id']), 'category_name');
                    $value['type'] = $type[$value['type']];
                }
            }

            include $this->template('goods');
        }

    }


    public function doWebWarehouse()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == 'addshow' || $op == 'editshow') {
            $warehouse = p_get('warehouse', array('id' => $id));
            include $this->template('warehouse_add');
        } elseif ($op == 'add' || $op == 'edit') {
            $data = array(
                'share_num' => $this->get('share_num'),
                'level' => $this->get('level'),
                'warehouse' => $this->get('warehouse'),
            );
            $bool = $id ? p_update('warehouse', $data, array('id' => $id)) : p_insert('warehouse', $data);
            message($bool ? "操作成功": "操作失败", $this->createWebUrl('warehouse'));
        } elseif($op == 'sort') {
            $sort = (int)$this->get('sort');
            $bool = p_update('warehouse', array('sort' =>$sort ), array('id'=> $id));
            json('', $bool ? 1 : 0);
        } elseif ($op == 'delete'){
            $bool = p_delete('warehouse', array('id' => $id));
            json('', $bool ? 1 : 0);
        } else {
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            $warehouse = $this->manger->getTableList('warehouse',$page, $where);
            include $this->template('warehouse');
        }

    }


    public function doWebSignInReward()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == 'add' || $op == 'edit') {
            $data = array(
                'days' => (int)$this->get('days'),
                'type' => $this->get('type'),
                'reward' => $this->get('reward'),
            );
            if (!$id){
                $data['created']= time();
                if (p_get('signin_reward', array('days'=> $data['days']))) {
                    message("天数已经存在请重新添加", $this->createWebUrl('signInReward', array('op' => 'addShow')), 'warning');
                }
            }
            if ($data['days'] < 1 || $data['days'] > 7) {
                message("天数必须是在1-7区间之前", $this->createWebUrl('signInReward', array('op' => 'addShow')), 'warning');
            }
            $bool = $id ? p_update('signin_reward', $data, array('id' => $id)) : p_insert('signin_reward', $data);
            message($bool ? "操作成功": "操作失败", $this->createWebUrl('signInReward'));
        } elseif ($op == 'addShow' || $op == 'editShow') {
            $signInReward = p_get('signin_reward', array('id' => $id));
            include $this->template('signin_reward_add');
        } elseif($op == 'delete') {
            $bool = p_update('signin_reward', array('is_delete' => 2), array('id'=> $id));
            json('', $bool ? 1 : 0);
        } else {
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            $signInReward = $this->manger->getTableList('signin_reward',$page, $where);
            include $this->template('signin_reward');
        }
    }

    public function doWebOrder()
    {

        $op = $this->get('op');
        if ($op == "editshow") {
            $order = p_get('order', array('id' => $this->get('id')));
            $order['goods_name'] = p_getcolumn('goods', array('id' => $order['goods_id']), 'goods_name');
            $order['nickname'] = p_getcolumn('member', array('id' => $order['member_id']), 'nickname');
            include $this->template('order_edit');
        } elseif ($op =="edit") {
            $id             = $this->get('id');
            $courier_name   = $this->get('courier_name');
            $courier_number = $this->get('courier_number');
            if (empty($id) || empty($courier_name) || empty($courier_number)) {
                message('发货信息不正确,请您检查你的发货信息',$this->createWebUrl('order'));
            }
            $treasure_order = array(
                'courier_name' => $courier_name,
                'courier_number' => $courier_number,
                'delivery_time' => time(),
                'status' => 2
            );
            $bool = p_update('order', $treasure_order, array('id' => $id));
            message($bool ? '发货成功' :'发货失败',$this->createWebUrl('order'));
        } elseif($op == 'success') {
            $id = $this->get('id');
            $bool = p_update('order', array('complete_time' => time(),'status' => 3), array('id' => $id));
            json('', $bool ? 1 : 0);
        } elseif($op == 'cancel') {
            $id = $this->get('id');
            $bool = p_update('order', array('status' => 4), array('id' => $id));
            json('', $bool ? 1 : 0);
        } else {
            $pindex = max(1, intval($this->get('page')));
            $psize = 20;
            $table = prefix('order');
            $where = "is_pay =2 AND uniacid={$this->w['uniacid']}";

            if ($status = $this->get('status')) {
                $where .= " AND status = {$status}";
            }
            $nickname = $this->get('nickname');
            if ($nickname) {
                $where .= " AND member_id in ({$this->manger->getMemberId($nickname)})";
            }

            $list = p_fetchall("SELECT * FROM {$table} WHERE {$where} ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize.','.$psize);
            if ($list) {
                $status = array(
                    1 => '未发货',
                    2 => '已发货',
                    3 => '已完成',
                    4 => '已取消',
                    5 => '已退款',
                );
                foreach ($list as & $value) {
                    $value['member'] = p_get('member', array('id' => $value['member_id']));
                    $value['goods_name'] = p_getcolumn('goods', array('id' => $value['goods_id']), 'goods_name');
                    $value['status'] = $status[$value['status']];
                }
            }
            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM {$table} WHERE {$where}");
            $pager = pagination($total, $pindex, $psize);
            include $this->template('order');
        }

    }


    public function doWebRule()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == 'addShow' || $op == 'editShow') {
            $rule = p_get('rule', array('id' => $id));
            //$rule['content'] = $rule['content'] ? json_decode($rule['content']) : array(0 => '');
            include $this->template('rule_add');
        } elseif ($op == 'add' || $op == 'edit') {
            $data = array(
                'content' => $this->get('content'),
         /*       'content' => json_encode($this->get('content')),*/
                'position' => $this->get('position'),
                'sort' => $this->get('sort'),
            );
            if (!$id) {
                $data['created'] = time();
            }
            $bool = $id ? p_update('rule', $data , array('id' => $id)) : p_insert('rule', $data);
            message($bool ? '操作成功': '操作失败', $this->createWebUrl('rule'), $bool ? 'success': 'error');
        } elseif($op == 'sort'){
            $sort = (int)$this->get('sort');
            $bool = p_update('rule', array('sort' =>$sort ), array('id'=> $id));
            json('', $bool ? 1 : 0);
        } elseif($op == 'delete'){
            $bool = p_delete('rule', array('id' => $id));
            json('', $bool ? 1 : 0);
        } else {
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            $rule = $this->manger->getTableList('rule',$page, $where);

            include $this->template('rule');
        }

    }

    public function doWebProgram()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == 'addShow' || $op == 'editShow') {
            $program = p_get('program', array('id' => $id));
            include $this->template('program_add');
        } elseif ($op == 'add' || $op == 'edit') {
            $data = array(
                'icon' => $this->get('icon'),
                'name' => $this->get('name'),
                'appid' => $this->get('appid'),
                'path' => $this->get('path'),
                'position' => $this->get('position'),
                'sort' => $this->get('sort'),
                'type' => $this->get('type'),
            );
            if (!$id) {
                $data['created'] = time();
            }
            $bool = $id ? p_update('program', $data , array('id' => $id)) : p_insert('program', $data);
            message($bool ? '操作成功': '操作失败', $this->createWebUrl('program'), $bool ? 'success': 'error');
        } elseif($op == 'sort'){
            $sort = (int)$this->get('sort');
            $bool = p_update('program', array('sort' =>$sort ), array('id'=> $id));
            json('', $bool ? 1 : 0);
        } elseif($op == 'delete'){
            $bool = p_delete('program', array('id' => $id));
            json('', $bool ? 1 : 0);
        } else {
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            $program = $this->manger->getTableList('program',$page, $where);
            $isPopularitye = $this->rising->isPopularitye();
            include $this->template('program');
        }

    }




    public function doWebAdvertisement()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == 'addShow' || $op == 'editShow') {
            $advertisement = p_get('advertisement', array('id' => $id));
            include $this->template('advertisement_add');
        } elseif ($op == 'add' || $op == 'edit') {
            $data = array(
                'image' => $this->get('image'),
                'title' => $this->get('title'),
                'type' => $this->get('type'),
                'appid' => $this->get('appid'),
                'url' => $this->get('url'),
                'sort' => $this->get('sort'),
                'status' => $this->get('status'),
            );
            if (!$id) {
                $data['created'] = time();
            }
            $bool = $id ? p_update('advertisement', $data , array('id' => $id)) : p_insert('advertisement', $data);

            message($bool ? '操作成功': '操作失败', $this->createWebUrl('advertisement'), $bool ? 'success': 'error');
        } elseif($op == 'sort'){
            $sort = (int)$this->get('sort');
            $bool = p_update('advertisement', array('sort' =>$sort ), array('id'=> $id));
            json('', $bool ? 1 : 0);
        } elseif($op == 'delete'){
            $bool = p_delete('advertisement', array('id' => $id));
            json('', $bool ? 1 : 0);
        } else {
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            $advertisement = $this->manger->getTableList('advertisement',$page, $where);
            include $this->template('advertisement');
        }

    }

    protected function getImages($id , $images)
    {
        if (!$id) {
            return getImage($images);
        }
        if ($id) {
            if(strpos($images,'http') !== false){
                return $images;
            }else{
                return getImage($images);
            }
        }

    }
    public function doWebVideoCategory()
    {
        $id = $this->get('id');
        $op = $this->get('op');
        if ($op == 'addShow' || $op == 'editShow') {
            $videoCategory = p_get('video_category', array('id' => $id));
            include $this->template('videoCategory_add');

        } elseif ($op == 'add' || $op == 'edit') {
            $data = array(
                'name' => $this->get('name'),
                'sort' => $this->get('sort'),
                'status' => $this->get('status'),
            );
            if (!$id) {
                $data['created'] = time();
            }
            $bool = $id ? p_update('video_category', $data, array('id' => $id)) : p_insert('video_category', $data);
            message($bool ? '操作成功': '操作失败', $this->createWebUrl('videoCategory'), $bool ? 'success': 'error');
        } elseif($op == 'sort') {
            $sort = $this->get('sort');
            $bool = p_update('video_category', array('sort' => $sort), array('id' => $id));
            json('', $bool ? 1 : 0);
        } elseif($op == 'delete') {
            $bool = p_delete('video_category', array('id' => $id));
            json('', $bool ? 1 :0);
        } else {
            $table = prefix('video_category');
            $where = "uniacid = {$this->w['uniacid']} AND 1 = 1";
            $pindex = max(1, intval($this->get('page')));
            $psize = 20;
            $list = pdo_fetchall('SELECT * FROM '.$table." WHERE {$where} ORDER BY id DESC LIMIT ".($pindex - 1) * $psize.','.$psize);
            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM {$table} WHERE {$where}");
            $pager = pagination($total, $pindex, $psize);
            include $this->template('videoCategory');
        }
    }


    public function doWebVideoAuthor()
    {
        $id = $this->get('id');
        $op = $this->get('op');
        if ($op == 'addShow' || $op == 'editShow') {
            $videoAuthor = p_get('video_author', array('id' => $id));
            include $this->template('videoAuthor_add');

        } elseif ($op == 'add' || $op == 'edit') {
            $data = array(
                'name' => $this->get('name'),
                'head' => $this->get('head'),
            );
            if (!$id) {
                $data['created'] = time();
            }
            $bool = $id ? p_update('video_author', $data, array('id' => $id)) : p_insert('video_author', $data);
            message($bool ? '操作成功': '操作失败', $this->createWebUrl('videoAuthor'), $bool ? 'success': 'error');
        } elseif($op == 'sort') {
            $sort = $this->get('sort');
            $bool = p_update('video_author', array('sort' => $sort), array('id' => $id));
            json('', $bool ? 1 : 0);
        } elseif($op == 'delete') {
            $bool = p_delete('video_author', array('id' => $id));
            json('', $bool ? 1 :0);
        } else {
            $table = prefix('video_author');
            $where = "uniacid = {$this->w['uniacid']} AND 1 = 1";
            $pindex = max(1, intval($this->get('page')));
            $psize = 20;
            $list = pdo_fetchall('SELECT * FROM '.$table." WHERE {$where} ORDER BY id DESC LIMIT ".($pindex - 1) * $psize.','.$psize);
            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM {$table} WHERE {$where}");
            $pager = pagination($total, $pindex, $psize);
            include $this->template('videoAuthor');
        }
    }


    public function doWebTimeSlot()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == "addShow" || $op == 'editShow') {
            $timeSlot = p_get('time_slot', array('id' => $id));
            include $this->template('time_slot_add');
        } elseif ($op == 'add' || $op == 'edit') {
            $data = array(
                'start' => $this->get('start'),
                'end'   => $this->get('end'),
            );
            $bool = !empty($id) ? p_update('time_slot', $data , array('id' => $id)) : p_insert('time_slot', $data);
            message($bool ? '操作成功': '操作失败', $this->createWebUrl('timeSlot'), $bool ? 'success': 'error');
        }  elseif ($op == 'delete') {
            $id = $this->get('id');
            p_delete('time_slot', array('id' => $id));
            json('1');
        }else {
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            $timeSlot = $this->manger->getTableList('time_slot',$page, $where);
            include $this->template('time_slot');
        }
    }



    public function doWebSubsection()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        if ($op == "addShow" || $op == 'editShow') {
            $subsection = p_get('subsection', array('id' => $id));
            include $this->template('subsection_add');
        } elseif ($op == 'add' || $op == 'edit') {
            $data = array(
                'start_time' => (int)$this->get('start_time'),
                'end_time'   => (int)$this->get('end_time'),
                'currency'   => $this->get('currency'),
                'describe'   => $this->get('describe'),
            );
            if (!$id) {
                $data['created'] = time();
            }
            if ($data['start_time'] >= $data['end_time']) {
                message( '打卡开始时间不能大于等于结束时间', $this->createWebUrl('subsection'),  'warning');
            }
            $bool = !empty($id) ? p_update('subsection', $data , array('id' => $id)) : p_insert('subsection', $data);
            message($bool ? '操作成功': '操作失败', $this->createWebUrl('subsection'), $bool ? 'success': 'error');
        }  elseif ($op == 'delete') {
            $id = $this->get('id');
            p_delete('subsection', array('id' => $id));
            json('1');
        } else {
            $page   = $this->get('page');
            $where  = "1 = 1 ".get_uniacid();
            $subsection = $this->manger->getTableList('subsection',$page, $where);
            include $this->template('subsection');
        }
    }


    public function doWebNavigation()
    {
        $op = $this->get('op');
        $id = $this->get('id');
        $siteRoot = $this->siteRoot;
        $modular_name = $this->gpc['m'];
        $graphic = $this->rising->isGraphic() ? 1 : 0;
        if ($op == 'add' || $op == 'edit') {
            $data = array(
                'name'      => $this->get('name'),
                'position'  => $this->get('position'),
                'path'      => $this->get('path'),
                'selection_image'  => $this->get('selection_image'),
                'unchecked_images' => $this->get('unchecked_images'),
                'selection_color'  => $this->get('selection_color'),
                'unchecked_color'  => $this->get('unchecked_color'),
                'sort'             => $this->get('sort'),
            );
            if (!$id) {
                $data['created'] = time();
            }
            $bool = $id ? p_update('navigation', $data, array('id' => $id)) : p_insert('navigation', $data);
            message($bool ? '操作成功': '操作失败', $this->createWebUrl('navigation'), $bool ? 'success': 'error');

        } elseif ($op == 'editShow' || $op == 'addShow') {
            $navigation = p_get('navigation', array('id' => $id));
            include $this->template('navigation_add');
        } elseif($op == 'sort') {
            $sort = $this->get('sort');
            $bool = p_update('navigation', array('sort' => $sort), array('id' => $id));
            json('', $bool ? 1 : 0);
        } elseif ($op == 'delete') {
            $bool = p_delete('navigation', array('id' => $id));
            json('', $bool ? 1 : 0);
        } else {
            $table = prefix('navigation');
            $where = "uniacid = {$this->w['uniacid']} AND 1 = 1";
            $pindex = max(1, intval($this->get('page')));
            $psize = 20;
            $list = pdo_fetchall('SELECT * FROM '.$table." WHERE {$where} ORDER BY id DESC LIMIT ".($pindex - 1) * $psize.','.$psize);
            $position = array(
                'index_home'    => '首页',
                'my_index'      => '个人中心',
                'my_friend'     => '好友',
                'rank_today'    => '排行榜',
                'shop_index'    => '商城',
                'pdd_category'  => '多多进宝',
                'news_index'    => '图文',
            );
            if ($list) {
                foreach ($list as & $value) {
                    $value['position'] =$position[$value['position']];
                }
            }
            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM {$table} WHERE {$where}");
            $pager = pagination($total, $pindex, $psize);

            include $this->template('navigation');
        }

    }



    /**
     *
     * 地址信息
     * @param $member_id
     * @return bool
     */
    public function get_address($member_id)
    {
        $table = prefix('address');
        return p_fetchall("SELECT * FROM {$table} WHERE member_id = {$member_id}  ORDER BY address_id DESC");
    }

    /**
     * 获取商品ids
     * @param $treasure_name
     * @return bool
     */
    protected function getTreasureId($treasure_name)
    {
        $table = prefix('treasure');
        $where = '1=1';
        $where .= " AND name LIKE '%{$treasure_name}%' ";
        $treasureInfo =  p_fetchall("SELECT id FROM {$table} WHERE {$where} ORDER BY id DESC ");
        if ($treasureInfo) {
            foreach ($treasureInfo as $item) {
                $treasure_id[] = $item['id'];

            }
            return implode(',', $treasure_id);
        }
        return '';
    }
    public function checkCode($code = '')
    {
        if (p_get('treasure', array('issue_number' => $code))) {
            return $this->checkCode($this->getIssueNumber(12));
        }
        return  $code;
    }
    /**
     * @param $length
     * @return string
     */
    protected function getIssueNumber($length)
    {
        $str ='201812345678998765412307899638527411236987412';
        $len =strlen($str)-1;
        $randStr='';
        for($i=0;$i<$length;$i++){
            $num=mt_rand(0,$len);
            $randStr .= $str[$num];
        }
        $str = date('Ymd') . $randStr;
        return $str;
    }
    public function doWebEditorUpload()
    {
        if (!isset($_FILES['imgFile'])) {
            json('请上传图片', 0);
        }
        load()->func('file');
        $reslut = file_upload($_FILES['imgFile']);
        if (isset($reslut['errno'])) {
            json($reslut['message'], 0);
        }
        $pic =  '/' . $reslut['path'];

        if (is_oss()) {
            $remotestatus = file_remote_upload($reslut['path']);
            if (is_error($remotestatus)) {
                json('远程附件上传失败', 0);
            }
        }

        die(json_encode(array('error' => 0, 'url' => getImage($pic))));
    }
    /**
     * 获取参数
     * @param $key
     * @param string $default
     * @return mixed|string
     *
     */
    private function get($key, $default = '')
    {
        return isset($this->gpc[$key]) ? $this->gpc[$key] : $default;
    }


    protected function pddMandate($pid)
    {
        if ($pid) {
            $url = 'http://gw-api.pinduoduo.com/api/router';
            $data = array(
                'type' => 'pdd.ddk.rp.prom.url.generate',
                'client_id' => getConfig('pdd_client_id'),
                'timestamp' => time(),
                'p_id_list' => json_encode(array($this->get('pdd_pid'))),
                'generate_qq_app' => true,
                'generate_we_app' => true,
            );
            $data['sign'] = autograph($data);
            $suessData = http_post($url, $data);
            var_dump($suessData);
        }
    }


    protected function isPddMandate()
    {

    }

}